﻿@page "/gateway/variable"
@namespace ThingsGateway.Gateway.Razor
@using ThingsGateway.Admin.Application
@using ThingsGateway.Admin.Razor
@using ThingsGateway.Gateway.Application
@attribute [Authorize]
@attribute [RolePermission]
@inherits ComponentDefault


<div style="height: var(--table-height);padding: 1px;margin:auto;">

    <AdminTable @ref=table TItem="Variable"
                EditDialogSize="Size.ExtraExtraLarge"
                AutoGenerateColumns="true"
                ShowAdvancedSearch=true
                AllowResizing="true"
                IsFixedHeader=true
                IsMultipleSelect=true
                CustomerSearchModel="@CustomerSearchModel"
                SearchMode=SearchMode.Top
                ShowExtendButtons=true
                ShowExportButton
                ShowDefaultButtons=true
                ExtendButtonColumnWidth=150
                OnQueryAsync="OnQueryAsync"
                IsPagination=true
                OnSaveAsync="Save"
                OnDeleteAsync="Delete">

        <CustomerSearchTemplate>
            @if (context is VariableSearchInput model)
            {
                @Render(model)
            }
        </CustomerSearchTemplate>

        <TableColumns>

            <TableColumn @bind-Field="@context.DeviceId" Align="Alignment.Center" Sortable=true>
                <Template Context="deviceId">
                    @{
                        CollectDeviceDict.TryGetValue(deviceId.Value!.Value, out var device);
                    }
                    @device?.Name
                </Template>

            </TableColumn>
        </TableColumns>
        <EditTemplate Context="context">
            <VariableEditComponent Model=context BusinessDevices="BusinessDeviceNames" CollectDevices="CollectDeviceNames" BusinessDeviceDict=BusinessDeviceDict CollectDeviceDict=CollectDeviceDict></VariableEditComponent>

        </EditTemplate>


        <ExportButtonDropdownTemplate Context="ExportContext">
            <Button class="dropdown-item" OnClick="() => ExcelExportAsync(ExportContext)" IsDisabled=@(!AuthorizeButton("导出"))>
                <i class="fas fa-file-export"></i>
                <span>@RazorLocalizer["TablesExportButtonExcelText"]</span>
            </Button>
            <Button class="dropdown-item" OnClick="() => ExcelImportAsync(ExportContext)" IsDisabled=@(!AuthorizeButton("导入"))>
                <i class="fas fa-file-import"></i>
                <span>@RazorLocalizer["TablesImportButtonExcelText"]</span>
            </Button>
        </ExportButtonDropdownTemplate>
        <TableToolbarTemplate>

            <TableToolbarButton TItem="Variable" IsShow=@(AuthorizeButton(AdminOperConst.Edit))
                                Color=Color.Info Text="@RazorLocalizer["BatchEdit"]"
                                OnClickCallback=@(BatchEdit) />


            <TableToolbarPopConfirmButton TItem="Variable"
                                          Color=Color.Warning Text="@Localizer["Clear"]" IsShow=@(AuthorizeButton("清空"))
                                          IsAsync OnConfirm=@(ClearVariableAsync) />

            <PopConfirmButton Color=Color.Warning Text="@Localizer["Test"]" IsDisabled=@(!AuthorizeButton("测试"))
                              IsAsync OnConfirm=@(InsertTestDataAsync)>

                <BodyTemplate>
                    <BootstrapInput @bind-Value=TestCount ShowLabel="true" ShowLabelTooltip="true" />
                </BodyTemplate>

            </PopConfirmButton>

        </TableToolbarTemplate>
    </AdminTable>
</div>

@code {
    AdminTable<Variable> table;
}
@code {
    RenderFragment Render(VariableSearchInput model) =>
    @<div class="row form-inline g-2">
        <div class="col-12 col-md-6">
            <Select IsVirtualize @bind-Value="model.DeviceId" Items="@CollectDeviceNames" ShowSearch="true" ShowLabel="true" IsClearable DisableItemChangedWhenFirstRender=true  />
        </div>
        <div class="col-12 col-md-6">
            <Select IsVirtualize @bind-Value="model.BusinessDeviceId" Items="@BusinessDeviceNames" ShowSearch="true" ShowLabel="true" IsClearable DisableItemChangedWhenFirstRender=true  />
        </div>
        <div class="col-12 col-md-6">
            <BootstrapInput @bind-Value="model.Name" ShowLabel="true" />
        </div>
        <div class="col-12 col-md-6">
            <BootstrapInput @bind-Value="model.RegisterAddress" ShowLabel="true" />
        </div>
    </div>;

}
